package fy.uap.operatorImpl;

import java.util.ArrayList;
import java.util.List;

import com.seeyon.oainterface.exportData.flow.FlowExport;
import com.seeyon.oainterface.exportData.form.ValueExport;

import fy.form.listen.dao.MyOrgManager;
import fy.form.listen.log.LogWriter;
import fy.form.listen.operate.AForm_Save_Operator;
import fy.form.listen.set.OA_UAPAccountContrast;
import fy.uap.bean.ProviderImportFormBean;
import fy.uap.dao.MergerProviderInfoFormDao;
import fy.util.MyUtil;

public class MergerProviderInfoFormOperator extends AForm_Save_Operator {

	private ProviderImportFormBean mpfb = new ProviderImportFormBean();
	private long memberID;
	private List<String> companyIDList = new ArrayList<String>();
	private ApptitudeReader apptitudeReader = new ApptitudeReader();

	public String getFormName() {
		return "供应商信息更改申请表";
	}

	public boolean save() {
		ProviderMessageManager pmm = new ProviderMessageManager();
		pmm.setMemberID(memberID);
		if (pmm.checkMemberIDIsExists(mpfb.getMemberCode())) {

			MergerProviderInfoFormDao mpfd = new MergerProviderInfoFormDao();

			mpfd.setMergerProviderFormBean(mpfb);
			mpfd.setCompanyList(companyIDList);
			mpfd.setApptitudeBeans(apptitudeReader.getApptitude());
			if (mpfd.save()) {
				if (!mpfd.isWriteAptitudeSuccess()) {
					StringBuilder sb = new StringBuilder()
							.append("《")
							.append(super.formBean.getFlowExport()
									.getFlowTitle())
							.append("》导入供应商资质发生异常，请手工录入。单据编号：")
							.append(mpfb.getNumber()).append("。");
					pmm.sendMessage(sb.toString());
					LogWriter.writeLog(MyUtil.getRealPath(this.getClass()),
							sb.toString());
				}
				return true;
			} else {
				String msg = new StringBuilder().append("《")
						.append(super.formBean.getFlowExport().getFlowTitle())
						.append("》导入UAP和ERP失败，单据编号：").append(mpfb.getNumber())
						.append("。原因：写入UAP数据库发生异常。请联系集团采购部相关人员，手工导入。")
						.toString();
				pmm.sendMessage(msg);
				LogWriter.writeLog(MyUtil.getRealPath(this.getClass()), msg);
			}
		} else {
			String msg = new StringBuilder().append("《")
					.append(super.formBean.getFlowExport().getFlowTitle())
					.append("》写入UAP和ERP失败，单据编号：").append(mpfb.getNumber())
					.append("。原因：填单人员工号为空。").toString();
			pmm.sendMessage(msg);
			LogWriter.writeLog(MyUtil.getRealPath(this.getClass()), msg);
		}
		return false;
	}

	@Override
	protected void parseOtherField_FirstStep(
			com.seeyon.v3x.services.flow.bean.FlowExport flowExport) {
		mpfb.setApplicationDate(flowExport.getFlowCreateTime());
		memberID = flowExport.getFlowCreatePerson().getId();
		mpfb.setMemberCode(MyOrgManager.getInstance().getMemberByID(memberID)
				.getCode());
	}

	@Override
	protected void parseValueExport(
			com.seeyon.v3x.services.form.bean.ValueExport ve) {
		String name = ve.getDisplayName();
		String value = ve.getValue();

		if ("供应商编码".equals(name)) {
			mpfb.setProviderCode(MyUtil.clearEmpty(value));
		} else if ("供应商名称".equals(name)) {
			mpfb.setProviderName(MyUtil.replaceXMLString(value));
		} else if ("单位选择器".equals(name)) {
			String uapAccount = OA_UAPAccountContrast.getUapAccountID(ve
					.getValue());
			companyIDList.add(uapAccount);
			setCompany(uapAccount);
		} else if ("原因说明".equals(name)) {
			mpfb.setReason(MyUtil.replaceXMLString(value));
		} else if ("同类供应商".equals(name)) {
			mpfb.setSimilarSuppliers(MyUtil.replaceXMLString(value));
		} else if ("企业地址".equals(name)) {
			mpfb.setAddress(MyUtil.replaceXMLString(value));
		} else if ("联系人".equals(name)) {
			mpfb.setLinkman(MyUtil.replaceXMLString(value));
		} else if ("供货周期".equals(name)) {
			mpfb.setSupplyCycle(MyUtil.replaceXMLString(value));
		} else if ("银行类别".equals(name)) {
			mpfb.setCvenbank(MyUtil.replaceXMLString(value));
		} else if ("发货地址".equals(name)) {
			mpfb.setSupplyAddress(MyUtil.replaceXMLString(value));
		} else if ("电话号码".equals(name)) {
			mpfb.setTel(MyUtil.replaceXMLString(value));
		} else if ("发票税码".equals(name)) {
			mpfb.setCpurbillcode(MyUtil.replaceXMLString(value));
		} else if ("银行账号".equals(name)) {
			mpfb.setBankAccount(MyUtil.replaceXMLString(value));
		} else if ("税务登记号".equals(name)) {
			mpfb.setTaxRegister(MyUtil.replaceXMLString(value));
		} else if ("传真号码".equals(name)) {
			mpfb.setFax(MyUtil.replaceXMLString(value));
		} else if ("物流方式PK".equals(name)) {
			mpfb.setCouttype(value);
		} else if ("币种PK".equals(name)) {
			mpfb.setCmoneytype(value);
		} else if ("质量体系".equals(name)) {
			mpfb.setQualitySystem(MyUtil.replaceXMLString(value));
		} else if ("邮编".equals(name)) {
			mpfb.setPostCode(MyUtil.replaceXMLString(value));
		} else if ("企业类型PK".equals(name)) {
			mpfb.setCompanyType(value);
		} else if ("付款条件".equals(name)) {
			mpfb.setCcuspaycond(MyUtil.replaceXMLString(value));
		} else if ("属性DF".equals(name)) {
			mpfb.setProperty(MyUtil.replaceXMLString(value));
		} else if ("电子邮箱".equals(name)) {
			mpfb.setEmail(MyUtil.replaceXMLString(value));
		} else if ("价格条款".equals(name)) {
			mpfb.setCcontent(MyUtil.replaceXMLString(value));
		} else if ("单据编号".equals(name)) {
			mpfb.setNumber(value);
		} else if ("开户银行".equals(name)) {
			mpfb.setcVenBankMemo(MyUtil.replaceXMLString(value));
		} else if ("银行开户名".equals(name)) {
			mpfb.setBankAccountName(MyUtil.replaceXMLString(value));
		} else if ("银行代码".equals(name)) {
			mpfb.setBankCode(MyUtil.replaceXMLString(value));
		} else if ("企业类别".equals(name)) {
			mpfb.setQylb(MyUtil.replaceXMLString(value));
		} else if ("注册资金".equals(name)) {
			mpfb.setZczj(MyUtil.replaceXMLString(value));
		} else if ("成立时间".equals(name)) {
			mpfb.setClsj(MyUtil.replaceXMLString(value));
		} else if ("职工总人数".equals(name)) {
			mpfb.setZgrs(MyUtil.replaceXMLString(value));
		} else if ("经营场所".equals(name)) {
			mpfb.setJycs(MyUtil.replaceXMLString(value));
		} else if ("法人代表".equals(name)) {
			mpfb.setFrdb(MyUtil.replaceXMLString(value));
		} else if ("法人代表身份证".equals(name)) {
			mpfb.setFrdbsfz(MyUtil.replaceXMLString(value));
		} else if ("年销售额".equals(name)) {
			mpfb.setNxse(MyUtil.replaceXMLString(value));
		} else {
			apptitudeReader.read(name, value);
		}
	}

	private boolean setCompany(String uapAccount) {
		// 福清万达/巴士/包边/福耀四公司供应商导入共享
		if ("1004".equals(uapAccount) || "1005".equals(uapAccount)) {
			if (companyIDList.indexOf("1004") == -1) {
				companyIDList.add("1004");
			}
			if (companyIDList.indexOf("1005") == -1) {
				companyIDList.add("1005");
			}
			if (companyIDList.indexOf("1024") == -1) {
				companyIDList.add("1024");
			}
			if (companyIDList.indexOf("1025") == -1) {
				companyIDList.add("1025");
			}
			return true;
		} else if ("1013".equals(uapAccount) || "1014".equals(uapAccount)) {
			// 长春轿车/ 巴士公司供应商导入共享
			if (companyIDList.indexOf("1013") == -1) {
				companyIDList.add("1013");
			}
			if (companyIDList.indexOf("1014") == -1) {
				companyIDList.add("1014");
			}
			return true;
		} else if ("1006".equals(uapAccount) || "1026".equals(uapAccount)) {
			// 广州汽车玻璃/广州佛山汽车玻璃导入共享
			if (companyIDList.indexOf("1006") == -1) {
				companyIDList.add("1006");
			}
			if (companyIDList.indexOf("1026") == -1) {
				companyIDList.add("1026");
			}
			return true;
		} else if ("1009".equals(uapAccount) || "1032".equals(uapAccount)) {
			// 湖北/武汉荆门导入共享
			if (companyIDList.indexOf("1032") == -1) {
				companyIDList.add("1032");
			}
			if (companyIDList.indexOf("1009") == -1) {
				companyIDList.add("1009");
			}
			return true;
		} else if ("1012".equals(uapAccount)) {
			// 北京 导入保定
			if (companyIDList.indexOf("1036") == -1) {
				companyIDList.add("1036");
			}
			return true;
		}
		return false;

	}
}
